rm(list = setdiff(ls(), lsf.str()))

#Load data
data_base<-haven::read_dta("Study 1/Original Data/ZA5665_a1_ea-ef_v26-0-0.dta")
data_replace<-haven::read_dta("Study 1/Original Data/ZA5665_d1_ea-ef_v26-0-0.dta")

#select variables
data_original <- data_base[,which(colnames(data_base)%in%c("z000001a", "ecbq042a", "ecbq043a", "ecbq044a", "ecbq045a", "ecbq046a", "ecbq047a", "ecbq048a", "ecbq049a", "ecbq050a", "ecbq051a", "ecbq052a", "ecbq053a", "ecbq054a", "ecbq055a", "ecbq056a", "ecbq057a", "ecbq058a", "ecbq059a", "ecbq060a", "ecbq061a", "ecbq062a", "ecbq063a", "ecbq064a", "ecbq065a", "ecbq066a", "ecbq067a", "ecbq068a", "ecbq069a", "ecbq070a", "ecbq071a", "z000011a", "ecbo084a", "ecbo087a", "ecbo083a", "ecbo101a", "ecbo102a", "ecbo103a", "ecbo104a", "ecbo105a", "ecbo106a", "efzh032c", "efzh031a", "efzh050c", "edzc151a", "efzh041a", "efaj110a", "efaj111a", "efaj112a", "efaj113a", "efaj114a", "efaj115a", "efaj090a", "eebu123a"))]
data_addition<- data_replace[,which(colnames(data_replace)%in%c("z000001a", "ecbq042a", "ecbq043a", "ecbq044a", "ecbq045a", "ecbq046a", "ecbq047a", "ecbq048a", "ecbq049a", "ecbq050a", "ecbq051a", "ecbq052a", "ecbq053a", "ecbq054a", "ecbq055a", "ecbq056a", "ecbq057a", "ecbq058a", "ecbq059a", "ecbq060a", "ecbq061a", "ecbq062a", "ecbq063a", "ecbq064a", "ecbq065a", "ecbq066a", "ecbq067a", "ecbq068a", "ecbq069a", "ecbq070a", "ecbq071a", "z000011a", "ecbo084a", "ecbo087a", "ecbo083a", "ecbo101a", "ecbo102a", "ecbo103a", "ecbo104a", "ecbo105a", "ecbo106a", "efzh032c", "efzh031a", "efzh050c", "edzc151a", "efzh041a", "efaj110a", "efaj111a", "efaj112a", "efaj113a", "efaj114a", "efaj115a", "efaj090a", "eebu123a"))]
data<-rbind(data_original, data_addition)

##Voted
data$didnotvote<-car::recode(data$eebu123a, "2=1; 1=0; -99=NA; -77=NA; -33=NA; -22=NA")

###Agreeableness---------------------------

#ecbq043a BIG-5: Be sensitive
data$agre_1<-car::recode(data$ecbq043a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")

#ecbq048a BiG-5: I'm rude
data$agre_2_rec<-car::recode(data$ecbq048a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$agre_2_rec<-6-data$agre_2_rec

#ecbq053a BiG-5: Give others confidence
data$agre_3<-car::recode(data$ecbq053a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")

#ecbq058a BiG-5: Others are indifferent to me
data$agre_4_rec<-car::recode(data$ecbq058a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$agre_4_rec<-6-data$agre_4_rec

#ecbq063a BiG-5: Meet others with respect
data$agre_5<-car::recode(data$ecbq063a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")

#ecbq068a BiG-5: Tend to criticize others
data$agre_6_rec<-car::recode(data$ecbq068a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$agre_6_rec<-6-data$agre_6_rec

#create scale
data$agre<-rowMeans(data.frame(data$agre_1, data$agre_2_rec, data$agre_3, data$agre_4_rec, data$agre_5, data$agre_6_rec), na.rm=T)


###Openness---------------

#ecbq046a BiG-5: Can inspire me for art
data$open_1<-car::recode(data$ecbq046a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
#ecbq056a BiG-5: Be originally
data$open_2<-car::recode(data$ecbq056a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
#ecbq066a BiG-5: Fun to think about things
data$open_3<-car::recode(data$ecbq066a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")

#ecbq051a BiG-5: Little interested in abstract considerations
data$open_4_rec<-car::recode(data$ecbq051a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$open_4_rec<-6-data$open_4_rec
#ecbq061a BiG-5: I'm not interested in art
data$open_5_rec<-car::recode(data$ecbq061a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$open_5_rec<-6-data$open_5_rec
#ecbq071a BiG-5: I'm not resourceful
data$open_6_rec<-car::recode(data$ecbq071a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$open_6_rec<-6-data$open_6_rec

data$open<-rowMeans(data.frame(data$open_1, data$open_2, data$open_3, data$open_4_rec, data$open_5_rec, data$open_6_rec), na.rm=T)

###Conscientiousness---------

#ecbq054a BiG-5: I am reliable
data$con_1<-car::recode(data$ecbq054a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
#ecbq059a BiG-5: Like clean
data$con_2<-car::recode(data$ecbq059a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
#ecbq064a BiG-5: Stick to a task
data$con_3<-car::recode(data$ecbq064a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
#ecbq044a BiG-5: Am messy
data$con_4_rec<-car::recode(data$ecbq044a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$con_4_rec<-6-data$con_4_rec
#ecbq049a BiG-5: Neige to postpone
data$con_5_rec<-car::recode(data$ecbq049a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$con_5_rec<-6-data$con_5_rec
#ecbq069a BiG-5: I'm a negligent
data$con_6_rec<-car::recode(data$ecbq069a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$con_6_rec<-6-data$con_6_rec

data$con<-rowMeans(data.frame(data$con_1, data$con_2, data$con_3, data$con_4_rec, data$con_5_rec, data$con_6_rec), na.rm=T)

###Extraversion--------
#ecbq047a BiG-5: Tend to guide
data$ext_1<-car::recode(data$ecbq047a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
#ecbq052a BiG-5: Bin full of energy
data$ext_2<-car::recode(data$ecbq052a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
#ecbq057a BiG-5: I am sociable
data$ext_3<-car::recode(data$ecbq057a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
#ecbq042a BIG-5: Am quiet 
data$ext_4_rec<-car::recode(data$ecbq042a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$ext_4_rec<-6-data$ext_4_rec
#ecbq062a BiG-5: Leave others to the decision
data$ext_5_rec<-car::recode(data$ecbq062a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$ext_5_rec<-6-data$ext_5_rec
#ecbq067a BiG-5: Less active than other
data$ext_6_rec<-car::recode(data$ecbq067a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$ext_6_rec<-6-data$ext_6_rec

data$ext<-rowMeans(data.frame(data$ext_1, data$ext_2, data$ext_3, data$ext_4_rec, data$ext_5_rec, data$ext_6_rec), na.rm=T)

#Neuroticism
#ecbq045a BiG-5: Worry me about
data$neu_1<-car::recode(data$ecbq045a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
#ecbq050a BiG-5: Bin depressed
data$neu_2<-car::recode(data$ecbq050a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
#ecbq070a BiG-5: React irritated
data$neu_3<-car::recode(data$ecbq070a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
#ecbq055a BiG-5: Bin balanced
data$neu_4_rec<-car::recode(data$ecbq055a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$neu_4_rec<-6-data$neu_4_rec
#ecbq060a BiG-5: Stay calm
data$neu_5_rec<-car::recode(data$ecbq060a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$neu_5_rec<-6-data$neu_5_rec
#ecbq065a BiG-5: I am confident
data$neu_6_rec<-car::recode(data$ecbq065a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$neu_6_rec<-6-data$neu_6_rec

data$neu<-rowMeans(data.frame(data$neu_1, data$neu_2, data$neu_3, data$neu_4_rec, data$neu_5_rec, data$neu_6_rec), na.rm=T)

#ecbo083a econ ideology ------
data$econ_cons <-car::recode(data$ecbo083a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; 98=NA; 2=0")

#anti-islam---------------
#ecbo101a: Constrain practice of Islam
data$anti_islam1 <-car::recode(data$ecbo101a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; 98=NA")
#ecbo102a: Islam fits into German society (reversed)
data$anti_islam2 <-car::recode(data$ecbo102a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; 98=NA")
data$anti_islam2<-8-data$anti_islam2
#ecbo103a: Presence of Muslims leads to conflicts
data$anti_islam3 <-car::recode(data$ecbo103a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; 98=NA")
#ecbo104a: Islamic communities should be surveilled by state
data$anti_islam4 <-car::recode(data$ecbo104a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; 98=NA")
#ecbo105a: No objection against a Muslim mayor (reversed)
data$anti_islam5 <-car::recode(data$ecbo105a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; 98=NA")
data$anti_islam5<-8-data$anti_islam5
#ecbo106a: Religious fanatics among Muslims
data$anti_islam6 <-car::recode(data$ecbo106a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; 98=NA")
data$social_cons<-rowMeans(data.frame(data$anti_islam1, data$anti_islam2, data$anti_islam3, data$anti_islam4, data$anti_islam5, data$anti_islam6), na.rm=T)

#age----
data$age <-car::recode(data$efzh032c, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$age<- 2017-data$age

#gender---
data$female<-car::recode(data$efzh031a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$female<-ifelse(data$female==2,1,0)

#income (personal only)-------------
data$dum<-car::recode(data$efzh050c, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA; 98=10")
data$income<-ifelse(data$dum<10, zero1(data$dum),
                    ifelse(data$dum==10, 2, NA))
data$income_missing<-ifelse(data$income==2,1,0)

#education
data$education<-car::recode(data$efzh041a, "-111=NA; -99=NA; -77=NA; -33=NA; -22=NA")
data$education<-ifelse(data$education==1,0,1)

#ED: vote populist in Germany----------------
data$vote_intention_populist<-ifelse(data$edzc151a==4 | data$edzc151a==6, 1, 0)

#Populist, other, not----
data$populist_other_not<-data$vote_intention_populist
data$populist_other_not[data$didnotvote==1]=2

#comparing voters of AfD with Die Linke and all other voters
data$vote_intention_populist_mlogit<-ifelse(data$edzc151a==4, 1, 
                                            ifelse(data$edzc151a==6, 2, 0))
data$vote_intention_populist_mlogit<-as.factor(data$vote_intention_populist_mlogit)
data$vote_intention_populist_mlogit <- relevel(data$vote_intention_populist_mlogit, ref = "0")
  

#Multinomial: populist vs. all other parties
#CDU/CSU; SPD; FDP; Greens (4); other (7); don't know 98
data$vote_populist_mlogit <-car::recode(as.numeric(data$edzc151a), "4=0; 6=0; 5=4; 7=5; 98=6; -99=NA; -88=NA; -77=NA; -44=NA; -33=NA; -22=NA")
data$vote_populist_mlogit<-as.factor(data$vote_populist_mlogit)
levels(data$vote_populist_mlogit) <- c("Populist","CDU/CSU","SPD", "FDP", "Greens", "Other", "Don't know")
data$vote_populist_mlogit <- relevel(data$vote_populist_mlogit, ref = "Populist")

#Multinomial: populist vs. government (CDU/CSU) and opposition (FDP; Greens, other).
data$vote_populist_mlogit_govopposition <-car::recode(as.numeric(data$edzc151a), "4=0; 6=0; 2=1; 3=2; 5=2; 7=2; 98=NA; -99=NA; -88=NA; -77=NA; -44=NA; -33=NA; -22=NA")
data$vote_populist_mlogit_govopposition<-as.factor(data$vote_populist_mlogit_govopposition)
levels(data$vote_populist_mlogit_govopposition) <- c("Populist","Government","Opposition")
data$vote_populist_mlogit_govopposition <- relevel(data$vote_populist_mlogit_govopposition, ref = "Populist")

#Left-right ideology
data$lr_ideology<-car::recode(data$efaj090a, "-111=NA; -99=NA; -33=NA; -22=NA; 98=NA")
data$lr_ideology<-zero1(data$lr_ideology)
data$lr_ideology[is.na(data$lr_ideology)==TRUE]=2
data$lr_ideology_missing<-ifelse(data$lr_ideology==2, 1,0)

#Save
save(data, file="Study 1/Altered Data/Study1_Germany.RData")
